//
//  GuideViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/20.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "GuideViewController.h"
#import "LoginViewController.h"
@interface GuideViewController ()<UIScrollViewDelegate>
{
    UIPageControl *pageControl;
    // 判断是否是第一次进入应用
    BOOL flag;
}
@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    for (int i = 0; i < 2; i ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"商户引导页-%d",i + 1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(size_width * i, 0, size_width, size_height))];
        imageView.image = image;
        [myScrollView addSubview:imageView];
        
        if (i == 1) {
            // 进入按钮
            UIButton *goButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
            goButton.frame = CGRectMake((size_width - 120)/2, size_height-120, 120, 32);
            [goButton setImage:[UIImage imageNamed:@"lijitiyian"] forState:(UIControlStateNormal)];
              [goButton setImage:[UIImage imageNamed:@"lijitiyian"] forState:(UIControlStateSelected)];
            [imageView addSubview:goButton];
            imageView.userInteractionEnabled = YES;
            [goButton addTarget:self action:@selector(go:) forControlEvents:(UIControlEventTouchUpInside)];
        }
    }
    myScrollView.bounces = NO;
    myScrollView.pagingEnabled = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.contentSize = CGSizeMake(size_width * 2, size_height);
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(size_width / 3, size_height * 15 / 17, size_width / 3, size_height / 16)];
    // 设置页数
    pageControl.numberOfPages = 2;
    // 设置页码的点的颜色
    pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"#EBC38E"];
    // 设置当前页码的点颜色
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"#ff9900"];
    
    [self.view addSubview:pageControl];

    
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
    pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}


// 点击按钮保存数据并切换根视图控制器
- (void)go:(UIButton *)sender {
    flag = YES;
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    // 保存用户数据
    [userDef setBool:flag forKey:@"notFirst"];
    [userDef synchronize];
    // 切换根视图控制器
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
    keyWindow.rootViewController = nav;
}

@end
